#import "sprite_game_enemy_1.h"

#import "sprite_game_bomb.h"

@implementation SpriteGameEnemy1

- (id)initSpriteGameEnemy1
{
	if ((self = [super initSpriteGameEnemy1]))
	{
		objectName_ = "object sprite game enemy type 1";
		
		/*box2d
		*/
		fixtureBody_ = NULL;
	}
	return self;
}

- (void)createBody
{
	int i;
	int _VertexCount;
	float _Ratio;
	b2Vec2 *_Vertex;
	b2PolygonShape _PolygonShape;
	b2FixtureDef _FixtureDef;
	
	[super createBody];
	
	_Ratio = 1 / CMgrGamePhysics::Ratio();
	_FixtureDef.density = 1.0f;
	_FixtureDef.isSensor = true;
	
	_VertexCount = g_CfgGameEnemyDataTable[1].box2d_collision_point_table_body.size();
	_Vertex = ptCreateCollisionVertex(&g_CfgGameEnemyDataTable[1].box2d_collision_point_table_body);
	for (i = 0; i < _VertexCount; ++i)
	{
		_Vertex[i] = ptScaleVector_same(_Vertex[i], _Ratio);
	}
	_PolygonShape.Set(_Vertex, _VertexCount);
	free(_Vertex);
	_FixtureDef.shape = &_PolygonShape;
	fixtureBody_ = box2dBody_->CreateFixture(&_FixtureDef);
}

- (void)destroyBody
{
	[super destroyBody];
	fixtureBody_ = NULL;
}

- (void)aiCreate
{
	switch (aiType_)
	{
	case ENEMY_AI_TYPE_0:
	case ENEMY_AI_TYPE_1: [self actionMovePath]; break;
	case ENEMY_AI_TYPE_2: [self actionMove]; break;
	case ENEMY_AI_TYPE_3: assert(false); break;
	default: assert(false);
	}
}

- (void)aiActionMoveOver
{
	switch (aiType_)
	{
	case ENEMY_AI_TYPE_0:
		break;
	case ENEMY_AI_TYPE_1:
		break;
	case ENEMY_AI_TYPE_2: [self actionAttack]; break;
	default: assert(false);
	}
}

- (void)aiActionStandOver
{
	switch (aiType_)
	{
	case ENEMY_AI_TYPE_0:
	case ENEMY_AI_TYPE_1: [self actionMovePath]; break;
	case ENEMY_AI_TYPE_2: [self actionMove]; break;
	case ENEMY_AI_TYPE_3: assert(false); break;
	default: assert(false);
	}
}

- (void)aiActionAttackOver
{
	switch (aiType_)
	{
	case ENEMY_AI_TYPE_0: break;
	case ENEMY_AI_TYPE_1: [self actionStand:standTime_]; break;
	case ENEMY_AI_TYPE_2: [self actionStand:rand() % 4 + 2]; break;
	case ENEMY_AI_TYPE_3: assert(false); break;
	default: assert(false);
	}
}

- (void)actionMove
{
	CGPoint endPos;
	CCSequence *seq;
	id actionMove;
	CCCallFunc *callBack;
	
	endPos = CfgGameGetPointCGPoint(rand() % 9);
	actionMove = tcCreateActionMove(self.positionInPixels, endPos, [self GetMoveSpeed]);
	callBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionMoveOver)];
	seq = [CCSequence actions:actionMove, callBack, nil];
	[self runAction:seq];
}

- (void)callBackActionAttackOver
{
	SpriteGameBomb *_Bomb;
	CGPoint _StartPos, _EndPos;
	float _Time;
	
	_StartPos = self.positionInPixels;
	_StartPos.y -= g_CfgGameEnemyDataTable[1].bomb_offset;
	_EndPos = [g_SceneGame.layerGameObject GetBridgePositionByX:self.positionInPixels.x];
	_Time = ptPointDistance_CGPoint(self.positionInPixels, _EndPos) / 90.0f;
	_Bomb = [[sprite_game_bomb alloc] initSpriteGameBomb:g_CfgGameEnemyDataTable[type_].bomb_type];
	[g_LayerGameObject AddBomb:_Bomb];
	_Bomb.positionInPixels = _StartPos;
	[_Bomb ActionAttack:_Time And:_EndPos];
}

@end